package cn.stylefeng.guns.modular.hp.servlet;


import cn.stylefeng.guns.modular.hp.model.FileDownloadInfo;
import cn.stylefeng.guns.modular.hp.service.IFileDownloadInfoService;
import cn.stylefeng.guns.modular.hp.utils.HttpUtils;
import com.alibaba.druid.util.StringUtils;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

public class DownloadFileServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {

        response.setContentType("application/x-download charset=UTF-8");
        java.io.FileInputStream fis = null;
        javax.servlet.ServletOutputStream sos = null;
        try {
            if (StringUtils.isEmpty(request.getParameter("fileid"))) {
                return;
            }
            String fileid = request.getParameter("fileid");

            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");

            IFileDownloadInfoService fileDownloadInfoService = WebApplicationContextUtils
                    .getWebApplicationContext(this.getServletContext()).getBean(IFileDownloadInfoService.class);

            FileDownloadInfo fileInfo =  fileDownloadInfoService.selectById(fileid);

            File file = null;

            if(fileInfo != null && fileInfo.getFilesize() != null){
                response.setHeader("Content-Length", fileInfo.getFilesize().toString());
                file = new File(fileInfo.getDirPath());
            }


            if (null == file || !file.exists()) {
                System.out.println("DownloadFileServlet:" + file.getAbsolutePath() + " 文件不存在!");
                return;
            }
            // 读取文件流
            fis = new java.io.FileInputStream(file);
            // 设置下载保存的文件名
            sos = response.getOutputStream();
            String showName = file.getName();
            String contentDisposition = HttpUtils.getContentDisposition(request, showName);
            response.setHeader("Content-Disposition", contentDisposition);
            int byteCount = 0;
            if (fis != null) {
                byte[] buff = new byte[1024];
                int bytesRead;
                while (-1 != (bytesRead = fis.read(buff, 0, buff.length))) {
                    sos.write(buff, 0, bytesRead);
                    sos.flush();
                    byteCount += bytesRead;
                }
            }
            sos.flush();
        } catch (IOException ioe) {
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (sos != null) {
                        sos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }


}
